#!/usr/bin/perl -w
use strict;
use Getopt::Long;

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

my $usage = <<'USAGE';

USAGE:

    panda_remove_sequence_source.pl [OPTIONS]... --sequence_src_name STRING --dbname STRING

    Mandatory arguments:
        --sequence_src_name STRING    the name of the sequence source
        --dbname STRING               the postgreSQL database name


    Optional arguments:

        [--user STRING]               the postgreSQL user name
        [--host STRING]               the postgreSQL database server
        [--password STRING]           the postgreSQL database password
        [--error_log FILE]            where to print errors
        [--verbose]                   explain what is being done
        [--stats]                     recalibrate database stats for (possibly) faster queries.

        [--help]                      display this help and exit


RESULTS:


    This script removes from the panda database all accession and sequence data derived
            from a specified sequence source.
    All data is archived.

USAGE


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory
my $sequence_src_name = '';
my $dbname			= '';
# optional parameters
my $help			= '';
my $user			= 'postgres';
my $password		= '';
my $host			= '';
my $error_file;
my $verbose;
my $stats;
my $help;
GetOptions(
			'sequence_src_name=s'	=> \$sequence_src_name,
			'dbname=s'				=> \$dbname,
			
			'user=s' 		   		=> \$user,
			'password'				=> \$password,
			'host=s'				=> \$host,
			
			'error_log=s'			=> \$error_log,
			'verbose'	 		    => \$verbose,
			'stats'		 		    => \$stats,
			'help'		 		    => \$help);

die $usage if ($help);
die "\nERROR:\n\tMissing Argument: dbname\n" .$usage
				unless ($dbname);
die "\nERROR:\n\tMissing Argument: sequence_src_name\n" .$usage
				unless ($sequence_src_name);

				
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Create command line for synch_panda_tables

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
$user		= '--user="'	. $user.'"' 	if ($user);
$host		= '--host="' 	. $host.'"' 	if ($host);
$password	= '--password="'. $password.'"'	if ($password);


$cmd_line  = "synch_panda_tables --dbname '$dbname' $user $host $password ";
$cmd_line .= ' -v' if ($verbose);
$cmd_line .= ' -s' if ($stats);
$cmd_line .= " -e $error_log" if ($error_log);
$cmd_line .= " --description 'Removing $sequence_src_name' ";


# remove trailing slash
$sql_cmds;
while (<DATA>)
{
	s/OBSOLETE_DB/$sequence_src_name/;
	sql_cmds .= $_;
}



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Execute SQL file and cleanup

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
open PROG, $cmd_line;
print PROG $sql_cmds;


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	SQL instructions for PSQL follows

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

__DATA__
INSERT INTO archived_accessions
     SELECT * from accessions
	 WHERE accessions.db_origin = 'OBSOLETE_DB';
DELETE FROM accessions
	 WHERE accessions.db_origin = 'OBSOLETE_DB';
